#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <signal.h>
#include <string.h>
#include <unistd.h>
#include <time.h>

#include "zlog.h"
#include "rsc.h"
//#include "sd.h"
#include "file.h"
#include "record.h"

static int demo_exit = 0;
static void sig_handle(int signo)
{
	if(signo == SIGINT)
		demo_exit = 1;
}

static int rand_in_range(int min, int max)
{
	int val = min;
	srand(time(NULL));
	
	val = rand()/RAND_MAX *(max-min) + min;
	return val;
}

#define DATA_BUF_SIZE (5*1024*1024)
static unsigned char video_buf[RSC_REC_MAX_CHN][DATA_BUF_SIZE];
static unsigned char audio_buf[RSC_REC_MAX_CHN][DATA_BUF_SIZE];
static unsigned char meta_buf[RSC_REC_MAX_CHN][DATA_BUF_SIZE];
/** pre process callback function define */
int video_preprocess(struct record_frame_info *frame,unsigned char *source)
{
	int len = rand_in_range(1024,1024*1024);
	unsigned char *data = source+len;
	
	if(len > frame->size)
		return -1;

	memcpy(frame->addr,data,len);
	frame->len = len;
	frame->pts = 0;
	frame->flag = 0;

	return 0;
}
int audio_preprocess(struct record_frame_info *frame,unsigned char *source)
{
	int len = rand_in_range(320,1024);
	unsigned char *data = source+len;
	
	if(len > frame->size)
		return -1;

	memcpy(frame->addr,data,len);
	frame->len = len;
	frame->pts = 0;
	frame->flag = 0;
	return 0;
}

int main(int argc,char *argv[])
{
	int ret = 0;

	fprintf(stdout,">>>start rsc demo...\n");
	if(zlog_init(RSC_LOG_CONF)<0)
	{
		fprintf(stderr,"zlog_init failed!\n");
		exit(EXIT_FAILURE);
	}
	zlog_category_t *zc_demo = zlog_get_category("demo");

	signal(SIGINT,sig_handle);
	//ret = rsc_init_storage(rsc_sd_dev_meta,rsc_sd_cont_meta);
	ret = rsc_init_storage(rsc_file_dev_meta,rsc_file_cont_meta);
	zlog_info(zc_demo,"ret = %d\n",ret);

	/** test record **/
	struct rsc_record_param rec_params;

	memset(&rec_params,0,sizeof(rec_params));
	/** fill record parameters */
	/// media param
	rec_params.media.video_cbr = 1;
	rec_params.media.video_bitrate = 1024000;
	rec_params.media.video_framerate = 25;
	rec_params.media.video_gop = 25;
	rec_params.media.video_codec = REC_CODEC_MJPEG;//REC_CODEC_H264;
	rec_params.media.video_width = 640;
	rec_params.media.video_height = 480;
	rec_params.media.pic_level = 5;

	rec_params.media.audio_sample_fmt = REC_SAMPLE_FMT_S16;
	rec_params.media.audio_sample_rate = 44100;
	rec_params.media.audio_channels = 2;
	rec_params.media.audio_bitrate = 64000;
	rec_params.media.audio_codec = REC_CODEC_PCM_ALAW;

	/// control param
	rec_params.control.on = 1;
	rec_params.control.file_time = 42*60;
	rec_params.control.file_pack = REC_PACK_AVI;
	strcpy(rec_params.control.file_format,"YYYYMMDD/hhmmss_nn.avi");
	rec_params.control.audio_on = 1;
	rec_params.control.audio_solo = 0;
	//rec_params.control.audio_time = 0;
	//rec_params.control.audio_pack = REC_PACK_WAV;
	rec_params.control.mode = REC_MODE_AUTO;
	strcpy(rec_params.control.mode_desc,"auto");

	/// data source config
	int i = 0;
	rec_params.source.chn = 4;
	rec_params.source.parallel = 0;// not used now
	for(i=0;i<rec_params.source.chn;i++)
	{
		rec_params.source.video_ds[i] = video_buf[i];
		rec_params.source.audio_ds[i] = audio_buf[i];
		rec_params.source.meta_ds[i] = meta_buf[i];
	}
	rec_params.source.preprocess[REC_DTYPE_VIDEO] = video_preprocess;
	rec_params.source.preprocess[REC_DTYPE_AUDIO] = audio_preprocess;
	rec_params.source.preprocess[REC_DTYPE_META] = NULL; // default is NULL

	/** end filling */
	ret = rsc_init_record(&rec_params);
	zlog_info(zc_demo,"test <rsc_init_record> api,ret = %d\n",ret);
	if(ret < 0) exit(1);

	while(!demo_exit)
	{
		sleep(1);
	}

	rsc_exit_record();
	rsc_exit_storage();

	zlog_fini();
	fprintf(stdout,"<<<exit rsc demo...\n");

	return 0;
}

